/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.ActivityEdge;
import org.eclipse.uml2.uml.LiteralString;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * Manages guard section of ActivityEdges
 *
 * @author Gilles Cannenterre
 * @since 4 avr. 07
 */
public class ActivityEdgeGuardSection extends PropertyViewSection {

	/**
	 * The Class DirectEditFocusListener.
	 */
	protected class DirectEditFocusListener implements org.eclipse.swt.events.FocusListener {
		
		/**
		 * Focus gained.
		 * 
		 * @param e the e
		 */
		public void focusGained(FocusEvent e){}
		
		/**
		 * Focus lost.
		 * 
		 * @param e the e
		 */
		public void focusLost(FocusEvent e){
			if((umlElement != null) && (umlElement instanceof ActivityEdge)) {			
				
				String activityEdgeGuard = guardText.getText().trim();
				((LiteralString)((ActivityEdge)umlElement).getGuard()).setValue(activityEdgeGuard);		
			}
		}
	}

	/**
	 * The weight text.
	 */
	private Text guardText;

	/**
	 * The direct edit focus listener.
	 */
	private DirectEditFocusListener directEditFocusListener = new DirectEditFocusListener();

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		
		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;
				
		guardText = this.getWidgetFactory().createText(composite, "");
		guardText.setEditable(true);
		// nameText.addFocusListener(directEditFocusListener);
		data = new FormData();
		data.width = 150;
		data.left = new FormAttachment(0, 85);
		data.right = new FormAttachment(100, -100);
		data.top = new FormAttachment(0, 0);
		guardText.setLayoutData(data);
		
		guardText.addFocusListener(directEditFocusListener);

		CLabel nameLabel = this.getWidgetFactory().createCLabel(composite, "Guard:");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(guardText,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(guardText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	@Override
	public void refresh() {
		if (!guardText.isDisposed()){
			if (umlElement instanceof ActivityEdge) {
				guardText.removeFocusListener(directEditFocusListener);
				if(((ActivityEdge)umlElement).getGuard() != null){
					if (((ActivityEdge)umlElement).getGuard() instanceof LiteralString){		
						if (((ActivityEdge)umlElement).getGuard().stringValue() != null){
							String guardString = ((ActivityEdge)umlElement).getGuard().stringValue();
							guardText.setText(guardString.trim());
						} else {
							guardText.setText("");
						}
					} else {
						guardText.setText("");
					}					
				} else {
					guardText.setText("");
				}
				guardText.addFocusListener(directEditFocusListener);
			}				
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if(guardText!=null && !guardText.isDisposed())
			guardText.removeFocusListener(directEditFocusListener);
	}
}